//
//  FTFriendsListViewController.m
//  FaceTo
//
//  Created by 姚冬元 on 2017/6/19.
//  Copyright © 2017年 chengbangming. All rights reserved.
//

#import "FTFriendsListViewController.h"
#import "FTSelFriendViewCell.h"

@interface FTFriendsListViewController ()<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UISearchBar *searchBar;

@property (nonatomic, strong) NSMutableArray *indexArray;

@property (nonatomic, strong) UIButton  *confirmBtn;

@end

@implementation FTFriendsListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
}


- (void)initUI
{
    self.title = @"选择好友";
    self.view.backgroundColor = WhiteColor;
    
    UIView *searchView = [[UIView alloc] init];
    searchView.frame = CGRectMake(0, 0, kSCREEN_WIDTH, 50);
    searchView.backgroundColor = WhiteColor;
    [self.view addSubview:searchView];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = LineColor;
    line.frame = CGRectMake(0, 49.3, kSCREEN_WIDTH, 0.7);
    [searchView addSubview:line];
    
    _searchBar = [[UISearchBar alloc] init];
    _searchBar.placeholder = @"搜索好友";
    _searchBar.delegate = self;
    _searchBar.backgroundColor = Color(243, 243, 243);
    _searchBar.layer.cornerRadius = 5.0f;
    [searchView addSubview:_searchBar];
    
    // 修改框体颜色
    for (UIView *view in _searchBar.subviews) {
        if ([view isKindOfClass:NSClassFromString(@"UIView")] && view.subviews.count > 0) {
            [[view.subviews objectAtIndex:0] removeFromSuperview];
            [_searchBar setBackgroundColor:Color(243, 243, 243)];
            break;
        }
    }
    for (UIView *subview in [[_searchBar.subviews lastObject] subviews]) {
        subview.backgroundColor = Color(243, 243, 243);
    }
    
    [_searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-10);
        make.left.offset(10);
        make.height.mas_equalTo(30);
        make.centerY.equalTo(searchView.mas_centerY);
    }];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, kSCREEN_WIDTH, kSCREEN_HEIGHT-50-64)];
    _tableView.backgroundColor = Color(237, 237, 237);
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:_tableView];
    
    _indexArray = [[NSMutableArray alloc] init];
    
    // nav右边按钮
    _confirmBtn = [[UIButton alloc] init];
    [_confirmBtn setTitle:@"邀请" forState:UIControlStateNormal];
    [_confirmBtn setTitleColor:BlackColor forState:UIControlStateNormal];
    _confirmBtn.titleLabel.font = LPFont(15);
    _confirmBtn.frame = CGRectMake(0, 0, 40, 40);
    _confirmBtn.userInteractionEnabled = NO;
    [_confirmBtn addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithCustomView:_confirmBtn];
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithCustomView:[UIButton buttonWithType:UIButtonTypeCustom]];
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:barBtn, item1, nil];
}

- (void)confirmBtnClick
{
    // 邀请
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    
    FTSelFriendViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell == nil) {
        cell = [[FTSelFriendViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.selectionStyle = NO;
    
    cell.block = ^(BOOL isSelected) {
        
        NSNumber *num = [NSNumber numberWithInteger:indexPath.row];
        
        if (isSelected) {
            
            [_indexArray containsObject:num] ? : [_indexArray addObject:num];
            
        } else {
            
            ![_indexArray containsObject:num] ? : [_indexArray removeObject:num];
        }
        
        for (NSNumber *num in _indexArray) {
            NSLog(@"%@", num);
        }
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self.view endEditing:YES];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Remove seperator inset
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    // Prevent the cell from inheriting the Table View's margin settings
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    // Explictly set your cell's layout margins
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}


@end
